<%-- Created by IntelliJ IDEA. User: chenjun Date: 2022/2/24 Time: 10:00 To change this template use File | Settings |
    File Templates. --%>
    <% String path=request.getContextPath(); String basePath=request.getScheme() + "://" + request.getServerName() + ":"
        + request.getServerPort() + path + "/" ; %>
        <%@ page contentType="text/html;charset=UTF-8" language="java" %>
            <!DOCTYPE html>
            <html>

            <head>
                <base href="<%=basePath%>">
                <title>采购方案</title>
                <meta http-equiv="pragma" content="no-cache">
                <meta http-equiv="cache-control" content="no-cache">
                <meta http-equiv="expires" content="0">
                <meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
                <meta http-equiv="description" content="This is my page">
            </head>
            <style>
                .tdText {
                    text-align: right;
                    width: 400px;
                    height: 20px;
                }

                input:disabled {
                    background-color: #dae1ed;
                }
            </style>

            <body>
                <%@ include file="/WEB-INF/jsp/common/header.jsp" %>
                    <%@ include file="/WEB-INF/jsp/common/loadDiv.jsp" %>

                        <script type="text/javascript">
                            $(function () {
                                $('#startTime').datebox('setValue', getBeforeCurrentDate30Days());
                                $('#endTime').datebox('setValue', getNowFormatDate());
                                bingGrid();
                                selectMMGrid();
                                suppliesDatagird();
                                // selectInquiryList();
                            });
                        </script>

                        <div class="query_criteria" id="query_criteria">
                            <div style="margin: 5px 0px">
                                <%-- 搜索框 --%>
                                    <label style="font-size: 12px;">创建时间：</label> <input class="easyui-datebox"
                                        name="startTime" id="startTime"
                                        data-options="required:true,showSeconds:false,formatter:myformatter,parser:myparser"
                                        value="" style="width: 150px"> <label style="font-size: 10px;">~</label> <input
                                        class="easyui-datebox" name="endTime" id="endTime"
                                        data-options="required:true,showSeconds:false,formatter:myformatter,parser:myparser"
                                        style="width: 150px" /> <label
                                        style="margin-left: 20px; font-size: 12px;">状态：</label> <input
                                        class="easyui-combobox" id="gramStatus" name="status" style="width: 80px;"
                                        data-options="valueField: 'value',editable:false,
		textField: 'label',panelHeight:'auto',data: [{
			label: '全部',
			value: ''
		},{
			label: '待提交',
			value: '0',
			'selected':true
		},{
			label: '已提交',
			value: '1'
		},
		{
			label: '已完成',
			value: '2'
		}]">
                                    <%-- <option value="">
                                        </option>
                                        <option value="0">待提交</option>
                                        <option value="1">已提交</option>--%>
                                        </select> <a id="search" style="margin-left: 30px" href="javascript:void(0)"
                                            class="easyui-linkbutton" data-options="iconCls:'icon-search'"
                                            onclick="selectList()">查询</a> <a id="export" style="margin-left: 10px"
                                            href="javascript:void(0)" class="easyui-linkbutton"
                                            data-options="iconCls:'icon-export'" onclick="exportList()">导出</a>
                            </div>
                            <%--采购方案列表DataGrid --%>
                                <table id="dataGridDiv"></table>
                                <%-- 详情弹窗 --%>
                                    <div id="gramDetailWindow" class="easyui-window" closed="true" modal="true"
                                        title="采购方案" style="width: 900px; height: 400px; padding: 5px 5px">
                                        <div id="tabs" class="easyui-tabs" style="width: 875px; height: 350px">
                                            <div title="基本信息" style="padding: 5px;">
                                                <form id="programForm">
                                                    <table>
                                                        <tr style="margin-bottom: 10px">
                                                            <td colspan="1" class="tdText"><label
                                                                    style="width: 40%; text-align: right;">方案编码：</label>
                                                                <input id="gramCode" class="easyui-validatebox textbox"
                                                                    data-options="disabled:true" style="width: 60%">
                                                                <input id="gramId" type="hidden"
                                                                    class="easyui-validatebox textbox"
                                                                    style="width: 60%">
                                                            </td>
                                                            <td colspan="1" class="tdText"><label
                                                                    style="width: 40%; text-align: right;">方案名称：</label>
                                                                <input id="gramName" class="easyui-validatebox textbox"
                                                                    data-options="required:true" style="width: 60%">
                                                            </td>
                                                            <!--                                 <td colspan="1" class="tdText"><label -->
                                                            <!--                                         style="width: 40%; text-align: right;">业务类别：</label> -->
                                                            <!--                                     <input id="gramType" class="easyui-combobox" data-options="required:true,url:'sysDict/queryItemByTypeName?dicName=业务类别',method:'get', -->
                                                            <!-- 					                                                          valueField:'dicValue',textField:'dicName',panelHeight:'auto'" style="width: 60%"> -->
                                                            <!--                                 </td> -->
                                                            <td colspan="1" class="tdText"><label
                                                                    style="width: 40%; text-align: right;">内部外部：</label>
                                                                <input id="gramIsInside" class="easyui-combobox"
                                                                    data-options="valueField: 'value',textField: 'label', editable:false,
		                                                                         panelHeight:'auto',data: [{label: '内部',value: '0',selected:true},{
			                                                                     label: '外部', value: '1'}]" style="width: 60%">
                                                        </tr>
                                                        <tr style="margin-bottom: 10px">

                                                            </td>
                                                            <!--                                 <td colspan="1" class="tdText"><label -->
                                                            <!--                                         style="width: 40%; text-align: right;">物资类型：</label> -->
                                                            <!--                                     <input id="gramMmType" class="easyui-combobox" data-options="required:true,url:'sysDict/queryItemByTypeName?dicName=物资类型',method:'get', -->
                                                            <!-- 					                                                          valueField:'dicValue',textField:'dicName',panelHeight:'auto'" style="width: 60%"> -->
                                                            <!--                                 </td> -->

                                                        </tr>
                                                        <tr style="margin-bottom: 10px">
                                                            <td colspan="1" class="tdText"><label
                                                                    style="width: 40%; text-align: right;">合同类型：</label>
                                                                <input id="gramContractType" class="easyui-combobox"
                                                                    data-options="valueField: 'value',textField: 'label', editable:false,
		                                                                         panelHeight:'auto',data: [{label: '采购协议',value: '采购协议',selected:true},{
			                                                                     label: '采购订单', value: '采购订单'}]"
                                                                    style="width: 60%">
                                                            </td>
                                                            <td colspan="1" class="tdText"><label
                                                                    style="width: 40%; text-align: right;">预算金额：</label>
                                                                <input id="gramBudget"
                                                                    class="easyui-validatebox textbox"
                                                                    style="width: 60%">
                                                            </td>
                                                        </tr>
                                                        <tr style="margin-bottom: 10px">
                                                            <td colspan="1" class="tdText"><label
                                                                    style="width: 40%; text-align: right;">采购方式：</label>
                                                                <input id="gramMode" class="easyui-combobox"
                                                                    data-options="required:true,url:'sysDict/queryItemByTypeName?dicName=采购方式',method:'get',
					                                                          valueField:'dicValue',textField:'dicName',panelHeight:'auto',editable:false"
                                                                    style="width: 60%">
                                                            </td>
                                                            <td colspan="1" class="tdText"><label
                                                                    style="width: 40%; text-align: right;">采购单位编码：</label>
                                                                <input id="gramDeptCode"
                                                                    class="easyui-validatebox textbox"
                                                                    style="width: 60%">
                                                            </td>
                                                            <td colspan="1" class="tdText"><label
                                                                    style="width: 40%; text-align: right;">采购单位名称：</label>
                                                                <input id="gramDeptName"
                                                                    class="easyui-validatebox textbox"
                                                                    style="width: 60%">
                                                            </td>
                                                        </tr>
                                                        <tr style="margin-bottom: 10px">
                                                            <td colspan="1" class="tdText"><label
                                                                    style="width: 40%; text-align: right;">是否急用：</label>
                                                                <input id="gramIsUrgent" class="easyui-combobox"
                                                                    data-options="valueField: 'value',textField: 'label', editable:false,
		                                                                         panelHeight:'auto',data: [{label: '否',value: '0',selected:true},{
			                                                                     label: '是', value: '1'}]" style="width: 60%">
                                                            </td>
                                                            <td colspan="1" class="tdText"><label
                                                                    style="width: 40%; text-align: right;">选商方式：</label>
                                                                <input id="gramChoseMode" class="easyui-textbox"
                                                                    style="width: 60%">
                                                            </td>
                                                            <td colspan="1" class="tdText"><label
                                                                    style="width: 40%; text-align: right;">成交方式：</label>
                                                                <input id="gramDealMode" class="easyui-combobox"
                                                                    data-options=" editable:false,url:'sysDict/queryItemByTypeName?dicName=成交方式',method:'get',
					                                                         valueField:'dicValue',textField:'dicName',panelHeight:'auto'"
                                                                    style="width: 60%">
                                                            </td>
                                                        </tr>
                                                        <tr style="margin-bottom: 10px">
                                                            <td colspan="1" class="tdText"><label
                                                                    style="width: 40%; text-align: right;">流程类型：</label>
                                                                <input id="gramFlowType"
                                                                    class="easyui-validatebox textbox"
                                                                    style="width: 60%">
                                                            </td>
                                                            <td colspan="1" class="tdText"><label
                                                                    style="width: 40%; text-align: right;">创建时间：</label>
                                                                <input id="createTime"
                                                                    class="easyui-validatebox textbox"
                                                                    data-options="disabled:true" style="width: 60%">
                                                            </td>
                                                            <td colspan="1" class="tdText"><label
                                                                    style="width: 40%; text-align: right;">创建人：</label>
                                                                <input id="createName"
                                                                    class="easyui-validatebox textbox"
                                                                    data-options="disabled:true" style="width: 60%">
                                                            </td>
                                                        </tr>
                                                        <tr style="margin-bottom: 10px">
                                                            <td colspan="1" class="tdText"><label
                                                                    style="width: 40%; text-align: right;">更新人：</label>
                                                                <input id="updateName"
                                                                    class="easyui-validatebox textbox"
                                                                    data-options="disabled:true" style="width: 60%">
                                                            </td>
                                                            <td colspan="1" class="tdText"><label
                                                                    style="width: 40%; text-align: right;">更新时间：</label>
                                                                <input id="updateTime"
                                                                    class="easyui-validatebox textbox"
                                                                    data-options="disabled:true" style="width: 60%">
                                                            </td>
                                                        </tr>
                                                        <tr>
                                                            <td colspan="1" class="tdText"><label
                                                                    style="width: 40%; text-align: right;">相关说明：</label>
                                                                <input id="gramExplain"
                                                                    class="easyui-validatebox textbox"
                                                                    data-options="readonly:false,multiline:true"
                                                                    style="width: 60%; height: 100px">
                                                            </td>
                                                        </tr>
                                                    </table>
                                                </form>
                                            </div>
                                            <div title="物资明细" id="gramMMPanel" style="padding: 5px;">
                                                <table id="planMmDetailList"></table>
                                            </div>
                                            <div title="供应商明细" style="padding: 5px;">
                                                <table id="suppliesList"></table>
                                            </div>
                                        </div>
                                        <div>
                                            <%-- 选择采购计划的弹窗 --%>
                                                <div id="selectPlanWindow" class="easyui-window" closed="true"
                                                    modal="true" title="采购计划"
                                                    style="width: 94%; height: 600px; padding: 5px 5px">
                                                    <div style="margin: 5px 0px">
                                                        <%-- 搜索框 --%>
                                                            <label style="font-size: 12px;">创建时间：</label> <input
                                                                class="easyui-datebox" name="startPlanTime"
                                                                id="startPlanTime"
                                                                data-options="required:true,showSeconds:false,formatter:myformatter,parser:myparser"
                                                                value="" style="width: 150px"> <label
                                                                style="font-size: 10px;">~</label> <input
                                                                class="easyui-datebox" name="endPlanTime"
                                                                id="endPlanTime"
                                                                data-options="required:true,showSeconds:false,formatter:myformatter,parser:myparser"
                                                                style="width: 150px" /> </select> <a id="search"
                                                                style="margin-left: 30px" href="javascript:void(0)"
                                                                class="easyui-linkbutton"
                                                                data-options="iconCls:'icon-search'"
                                                                onclick="selectList()">查询</a>
                                                    </div>
                                                    <%--采购计划列表DataGrid --%>
                                                        <table id="planDataGridWindow"></table>
                                                </div>

                                                <%-- 选择计划物资明细的弹窗 --%>
                                                    <div id="selectMMWindow" class="easyui-window" closed="true"
                                                        modal="true" title="计划物资"
                                                        style="width: 900px; height: 400px; padding: 5px 5px">
                                                        <div style="margin: 5px 0px">
                                                            <%-- 搜索框 --%>
                                                                <label style="font-size: 12px;">创建时间：</label> <input
                                                                    class="easyui-datebox" name="startMMTime"
                                                                    id="startMMTime"
                                                                    data-options="required:true,showSeconds:false,formatter:myformatter,parser:myparser"
                                                                    value="" style="width: 150px"> <label
                                                                    style="font-size: 10px;">~</label> <input
                                                                    class="easyui-datebox" name="endMMTime"
                                                                    id="endMMTime"
                                                                    data-options="required:true,showSeconds:false,formatter:myformatter,parser:myparser"
                                                                    style="width: 150px" /> </select> <a id="search"
                                                                    style="margin-left: 30px" href="javascript:void(0)"
                                                                    class="easyui-linkbutton"
                                                                    data-options="iconCls:'icon-search'"
                                                                    onclick="selectMMListByParams()">查询</a>
                                                        </div>
                                                        <%--计划物资列表DataGrid --%>
                                                            <table id="mmDataGrid"></table>
                                                    </div>

                                                    <%-- 选择供应商的弹窗 --%>
                                                        <div id="selectSupplyWindow" class="easyui-window" closed="true"
                                                            modal="true" title="待选供应商"
                                                            style="width: 900px; height: 400px; padding: 5px 5px">
                                                            <!-- 					<div style="margin: 5px 0px">  -->
                                                            <!-- 						<label style="font-size: 12px;">创建时间：</label> <input -->
                                                            <!-- 							class="easyui-datebox" name="startSupplyTime" -->
                                                            <!-- 							id="startSupplyTime" -->
                                                            <!-- 							data-options="required:true,showSeconds:false,formatter:myformatter,parser:myparser" -->
                                                            <!-- 							value="" style="width: 150px"> <label -->
                                                            <!-- 							style="font-size: 10px;">~</label> <input class="easyui-datebox" -->
                                                            <!-- 							name="endSupplyTime" id="endSupplyTime" -->
                                                            <!-- 							data-options="required:true,showSeconds:false,formatter:myformatter,parser:myparser" -->
                                                            <!-- 							style="width: 150px" /> </select> <a id="search" -->
                                                            <!-- 							style="margin-left: 30px" href="javascript:void(0)" -->
                                                            <!-- 							class="easyui-linkbutton" data-options="iconCls:'icon-search'" -->
                                                            <!-- 							onclick="selectSupplyListByParams()">查询</a> -->
                                                            <!-- 					</div> -->
                                                            <%--待选供应商列表DataGrid --%>
                                                                <table id="supplyDataGrid"></table>
                                                        </div>
                                        </div>
                                    </div>
                        </div>
                        </div>

            </body>
            <script type="text/javascript">
                var editIndex = undefined;

                // 行编辑结束
                function endEditing() {
                    if (editIndex == undefined) {
                        return true
                    }
                    if ($('#planMmDetailList').datagrid('validateRow', editIndex)) {
                        $('#planMmDetailList').datagrid('endEdit', editIndex);
                        editIndex = undefined;
                        return true;
                    } else {
                        $.messager.alert('消息', '请维护采购量!');
                        return false;
                    }
                }
                function cellStyler(value, row, index) {
                    return 'background-color:#ffee00;color:red;';
                }

                // 物资列表的列
                const arrMMColmus = [
                    [{
                        field: 'mmUnit',
                        title: '计量单位',
                        sortable: true,
                        formatter: function (val, rec) {
                            return unescape(val ? val : '')
                        }
                    },
                    {
                        field: 'planPurNum',
                        title: '采购计划量',
                        sortable: true,
                        formatter: function (val, rec) {
                            return unescape(val ? val : '')
                        }
                    },
                    // {
                    //     field: 'gramStockNum',
                    //     title: '剩余量',
                    //     sortable: true,
                    //     formatter: function (val, rec) {
                    //         return unescape(val ? val : '')
                    //     }
                    // },
                    {
                        field: 'gramPurNum',
                        title: '采购量',
                        width: 100,
                        editor: {
                            type: 'numberbox',
                            options: {
                                precision: 4,
                                required: true
                            }
                        },
                        formatter: function (val, rec) {
                            return unescape(val ? val : '')
                        }, styler: cellStyler
                    },
                    {
                        field: 'gramDevice',
                        title: '设备位号',
                        sortable: true,
                        formatter: function (val, rec) {
                            return unescape(val ? val : '')
                        }
                    },
                    {
                        field: 'gramQm',
                        title: '质量技术要求',
                        sortable: true,
                        formatter: function (val, rec) {
                            return unescape(val ? val : '')
                        }
                    },
                    {
                        field: 'gramUse',
                        title: '用途',
                        sortable: true,
                        formatter: function (val, rec) {
                            return unescape(val ? val : '')
                        }
                    },
                    {
                        field: 'gramUseName',
                        title: '使用人姓名',
                        sortable: true,
                        formatter: function (val, rec) {
                            return unescape(val ? val : '')
                        }
                    },
                    {
                        field: 'gramUseTel',
                        title: '使用人联系方式',
                        sortable: true,
                        formatter: function (val, rec) {
                            return unescape(val ? val : '')
                        }
                    },
                    {
                        field: 'gramOutType',
                        title: '出库类型',
                        sortable: true,
                        formatter: function (val, rec) {
                            return unescape(val ? val : '')
                        }
                    },
                    {
                        field: 'planCorpCode',
                        title: '使用公司编码',
                        sortable: true,
                        formatter: function (val, rec) {
                            return unescape(val ? val : '')
                        }
                    },
                    {
                        field: 'planCorpName',
                        title: '使用公司名称',
                        sortable: true,
                        formatter: function (val, rec) {
                            return unescape(val ? val : '')
                        }
                    },
                    {
                        field: 'planCostCode',
                        title: '成本中心编码',
                        sortable: true,
                        formatter: function (val, rec) {
                            return unescape(val ? val : '')
                        }
                    },
                    {
                        field: 'planCostDetail',
                        title: '成本中心描述',
                        sortable: true,
                        formatter: function (val, rec) {
                            return unescape(val ? val : '')
                        }
                    },
                    {
                        field: 'planFactoryCode',
                        title: '到货工厂',
                        sortable: true,
                        formatter: function (val, rec) {
                            return unescape(val ? val : '')
                        }
                    },
                    {
                        field: 'planFactoryName',
                        title: '到货工厂描述',
                        sortable: true,
                        formatter: function (val, rec) {
                            return unescape(val ? val : '')
                        }
                    },
                    {
                        field: 'planUnderDate',
                        title: '归口日期',
                        sortable: true,
                        formatter: function (val, rec) {
                            return unescape(val ? val : '')
                        }
                    },
                    {
                        field: 'createTime',
                        title: '创建时间',
                        sortable: true,
                        formatter: function (val, rec) {
                            return unescape(val ? val : '')
                        }
                    },
                    {
                        field: 'createName',
                        title: '创建人',
                        sortable: true,
                        formatter: function (val, rec) {
                            return unescape(val ? val : '')
                        }
                    }
                    ]
                ];
                // 采购计划的列
                const arrPlanColmus = [
                    [{
                        field: 'planId',
                        title: '采购计划ID',
                        hidden: true,
                        sortable: true,
                        width: 100,
                        formatter: function (val, rec) {
                            return unescape(val ? val : '')
                        }
                    },
                    {
                        field: 'planCode',
                        title: '计划编码',
                        sortable: true,
                        formatter: function (val, rec, index) {
                            return unescape(val ? val : '')
                        }
                        // }, {
                        //     field: 'planName',
                        //     title: '计划名称',
                        //     sortable: true,
                        //     formatter: function (val, rec) {
                        //         return unescape(val ? val : '')
                        //     }
                    }, {
                        field: 'planCategory',
                        title: '计划类别',
                        sortable: true,
                        formatter: function (val, rec) {
                            return unescape(val ? val : '')
                        }
                    },
                    {
                        field: 'planType',
                        title: '计划类型',
                        sortable: true,
                        formatter: function (val, rec) {
                            return unescape(val ? val : '')
                        }
                    },
                    {
                        field: 'planMmType',
                        title: '物资类型',
                        sortable: true,
                        formatter: function (val, rec) {
                            return unescape(val ? val : '')
                        }
                    },
                    {
                        field: 'planProCode',
                        title: '项目编码',
                        sortable: true,
                        formatter: function (val, rec) {
                            return unescape(val ? val : '')
                        }
                    },
                    {
                        field: 'planProName',
                        title: '项目名称',
                        sortable: true,
                        formatter: function (val, rec) {
                            return unescape(val ? val : '')
                        }
                    },
                    {
                        field: 'planPurType',
                        title: '采购类型',
                        sortable: true,
                        formatter: function (val, rec) {
                            return unescape(val ? val : '')
                        }
                    },
                    {
                        field: 'planDeptCode',
                        title: '使用单位编码',
                        sortable: true,
                        formatter: function (val, rec) {
                            return unescape(val ? val : '')
                        }
                    },
                    {
                        field: 'planDeptName',
                        title: '使用单位名称',
                        sortable: true,
                        formatter: function (val, rec) {
                            return unescape(val ? val : '')
                        }
                    },
                    {
                        field: 'planIsNeed',
                        title: '是否急用',
                        sortable: true,
                        formatter: function (val, rec) {
                            return unescape(val ? val : '')
                        }
                    },
                    {
                        field: 'createName',
                        title: '制单人名称',
                        sortable: true,
                        formatter: function (val, rec) {
                            return unescape(val ? val : '')
                        }
                    },
                    {
                        field: 'createTime',
                        title: '创建时间',
                        sortable: true,
                        formatter: function (val, rec) {
                            return unescape(val ? val : '')
                        }
                    },
                    {
                        field: 'updateTime',
                        title: '更新时间',
                        sortable: true,
                        formatter: function (val, rec) {
                            return unescape(val ? val : '')
                        }
                    },
                    {
                        field: 'status',
                        title: '状态',
                        sortable: true,
                        formatter: function (val, rec) {
                            let status = val ? val : '';
                            return unescape(status == '0' ? '待提交' : (status == '2' ? '已完成' : '已提交'));
                        }
                    }
                    ]
                ];
                //
                // 撤回
                function gramBackUp() {
                    let gramRows = $('#dataGridDiv').datagrid('getSelected');
                    if (!gramRows) {
                        $.messager.alert('提示', '请选择一条数据');
                        return
                    }
                    if (+gramRows.status == 0) {
                        $.messager.alert('提示', '该方案未提交，不允许撤回')
                        return
                    }
                    let gramId = gramRows.gramId;
                    const purchaseProgram = {
                        "gramId": gramId
                    }
                    $.ajax({
                        type: "post",
                        dataType: "json",
                        data: JSON.stringify(purchaseProgram),
                        contentType: "application/json",
                        url: "rd/program/backProgram",
                        success: function (data) {
                            if (data.code != 60200) {
                                $.messager.alert('消息', data.msg);
                            } else {
                                $.messager.alert('消息', data.msg);
                                bingGrid();

                            }
                        },
                        error: function (XMLHttpRequest, textStatus, errorThrown) {
                            $.messager.alert('消息', errorThrown);
                        }
                    })

                };

                // 新增采购方案
                function addProgram() {

                    $('#tabs').tabs({
                        tools: [{
                            text: '保存',
                            iconCls: 'icon-save',
                            handler: function () {
                                savePurchasePrograms('save');
                            }
                        }, {
                            text: '提交',
                            iconCls: 'icon-ok',
                            handler: function () {
                                savePurchasePrograms('submit')
                            }
                        }]
                    })
                    // 清空form
                    $('#programForm').form('clear');
                    // 清空物资明细
                    setMmsDatagird([], 'add')
                    let dg = $('#planMmDetailList');
                    dg.datagrid({
                        data: []
                    });
                    // 清空供应商明细
                    setSuppliesDatagird([], 'add');
                    let supply = $('#suppliesList');
                    supply.datagrid({
                        data: []
                    });
                    $('#gramDetailWindow').window('open');
                    $('#tabs').tabs('select', 0);
                }

                // 移除所选
                function removeMm(el) {
                    if (el == 'mm') {
                        let Rows = $('#planMmDetailList').datagrid('getSelected');
                        if (!Rows) {
                            $.messager.alert('提示', '请选择一条数据');
                            return
                        }
                        // 获取索引
                        let index = $('#planMmDetailList').datagrid('getRowIndex', Rows);
                        delProgramMm(Rows, index)
                    } else if (el == 'supply') {
                        let Rows = $('#suppliesList').datagrid('getSelected');
                        if (!Rows) {
                            $.messager.alert('提示', '请选择一条数据');
                            return
                        }
                        // 获取索引
                        let index = $('#suppliesList').datagrid('getRowIndex', Rows);
                        $('#suppliesList').datagrid('deleteRow', index)
                    }
                }


                /**
                 * 删除采购方案物资
                 * @param data
                 */
                function delProgramMm(row, index) {
                    console.log('row:', row)
                    if (!row.gramId) {
                        return
                    }
                    $.ajax({
                        type: 'delete',
                        dataType: 'json',
                        url: 'rd/program/delProgramMm?gramId=' + row.gramId + '&planId=' + row.planId,
                        success: function (data) {
                            $.messager.alert('消息', data.msg);
                            if (data.code == 60200) {

                                $('#planMmDetailList').datagrid('deleteRow', index)
                            }
                        },
                        error: function (XMLHttpRequest, textStatus, errorThrown) {
                            $.messager.alert('消息', errorThrown);
                        }
                    })

                }

                /**
                 * 构建待选择的供应商列表window
                 */
                function setSuppliesDatagirdBySelection() {
                    $('#supplyDataGrid').datagrid('clearChecked');
                    selectSupplyListByParams()
                    $('#selectSupplyWindow').window('open');

                }

                function suppliesDatagird() {
                    //         $('#startSupplyTime').datebox('setValue', getBeforeCurrentDate30Days());
                    //         $('#endSupplyTime').datebox('setValue', getNowFormatDate());
                    let supplyDataGrid = $('#supplyDataGrid');
                    supplyDataGrid.datagrid({
                        queryParams: {
                            //                 startTime: $('#startTime').datebox('getValue') ? $('#startTime').datebox('getValue') : '',
                            //                 endTime: $('#endTime').datebox('getValue') ? $('#endTime').datebox('getValue') : ''
                            status: '1'
                        },
                        fitColumns: true,
                        url: 'supply/info/selectList',
                        iconCls: 'icon-application',
                        clientPaging: false,
                        checkOnSelect: true,
                        remoteFilter: true,
                        filterDelay: 0,
                        width: 860,
                        height: 300,
                        nowrap: true,
                        rownumbers: true,
                        animate: false,
                        collapsible: false,
                        singleSelect: false,
                        pageSize: 6,
                        pageList: [6, 18, 28, 50],
                        remoteSort: true,
                        pagination: true,
                        frozenColumns: [[{
                            field: 'ck',
                            checkbox: true,
                            width: 100,
                        },
                        {
                            field: 'infId',
                            title: '供应商id',
                            hidden: true,
                            sortable: true,
                            width: 100,
                            formatter: function (val, rec) {
                                return unescape(val ? val : '')
                            }
                        },
                        {
                            field: 'gramId',
                            title: '方案id',
                            hidden: true,
                            sortable: true,
                            width: 100,
                            formatter: function (val, rec) {
                                return unescape(val ? val : '')
                            }
                        },
                        {
                            field: 'infCode',
                            title: '供应商编码',
                            sortable: true,
                            formatter: function (val, rec, index) {
                                return unescape(val ? val : '')
                            }
                        }, {
                            field: 'infName',
                            title: '名称',
                            sortable: true,
                            formatter: function (val, rec) {
                                return unescape(val ? val : '')
                            }
                        },]],
                        columns: [
                            [
                                {
                                    field: 'infCreditCode',
                                    title: '社会信用代码',
                                    sortable: true,
                                    formatter: function (val, rec) {
                                        return val ? unescape(val) : '';
                                    }
                                }, {
                                    field: 'infCountry',
                                    title: '国家',
                                    sortable: true,
                                    formatter: function (val, rec) {
                                        return val ? unescape(val) : '';
                                    }
                                }, {
                                    field: 'infProvince',
                                    title: '地区',
                                    sortable: true,
                                    formatter: function (val, rec) {
                                        return val ? unescape(val) : '';
                                    }
                                }, {
                                    field: 'infCity',
                                    title: '地市',
                                    sortable: true,
                                    formatter: function (val, rec) {
                                        return val ? unescape(val) : '';
                                    }
                                }, {
                                    field: 'infAddress',
                                    title: '单位地址',
                                    sortable: true,
                                    formatter: function (val, rec) {
                                        return val ? unescape(val) : '';
                                    }
                                }, {
                                    field: 'infMobile',
                                    title: '手机',
                                    sortable: true,
                                    formatter: function (val, rec) {
                                        return val ? unescape(val) : '';
                                    }
                                }, {
                                    field: 'infContactsName',
                                    title: '联系人姓名',
                                    sortable: true,
                                    formatter: function (val, rec) {
                                        return val ? unescape(val) : '';
                                    }
                                }
                            ]
                        ],
                        toolbar: [{
                            text: '添加',
                            iconCls: 'icon-ok',
                            handler: function () {
                                addRowsToProgramSupplyDetailList();
                                //  editRow();
                            }
                        }],
                        onLoadSuccess: function () { },
                    })
                    supplyDataGrid.datagrid('enableFilter');
                }

                /**
                 * 将选中的供应商添加到供应商列表
                 */
                function addRowsToProgramSupplyDetailList() {
                    let suppliesList = $('#supplyDataGrid');
                    const checkedRows = suppliesList.datagrid('getChecked');
                    if (!checkedRows || checkedRows.length < 1) {
                        $.messager.alert('提示', '请至少选择一条数据');
                        return
                    }
                    const planMmrows = $('#suppliesList').datagrid('getRows')
                    if (checkedRows.length > 0) {
                        for (let i in checkedRows) {
                            const result = planMmrows.some((item, index, arr) => {
                                console.log('item:', item)
                                console.log('checkedRows:', checkedRows[i].infId)
                                return item.infId == checkedRows[i].infId
                            })
                            if (!result) {
                                $('#suppliesList').datagrid('insertRow', {
                                    row: checkedRows[i]
                                })
                            }
                            else {
                                $.messager.alert('提示', '供应商已存在');
                                return
                            }
                        }
                    }
                    $('#selectSupplyWindow').window('close');
                }

                // planMM 转换programMM
                function convertToPrograMM(planMM) {
                    let programMM = {
                        mmId: planMM.mmId,
                        planId: planMM.planId,
                        mmCode: planMM.mmCode,
                        mmDetail: planMM.mmDetail,
                        mmCategory: planMM.mmCategory,
                        gramNeedNum: planMM.planNeedNum,
                        planPurNum: planMM.planPurNum,
                        gramPurNum: planMM.planPurNum,
                        mmMain: planMM.mmMain,
                        mmSecond: planMM.mmSecond,
                        mmNature: planMM.mmNature,
                        mmTypeCode: planMM.mmTypeCode,
                        mmTypeName: planMM.mmTypeName,
                        mmUnit: planMM.mmUnit,
                        mmUnitDetail: planMM.mmUnitDetail,
                        gramStockNum: planMM.planStockNum,
                        gramNeedDate: planMM.planNeedDate,
                        gramDevice: planMM.planDevice,
                        gramQm: planMM.planQm,
                        gramUse: planMM.planUse,
                        gramUseName: planMM.planUseName,
                        gramUseTel: planMM.planUseTel,
                        gramOutType: planMM.planOutType,
                        planTransfer: planMM.planTransfer,
                        gramRemark: planMM.planRemark,
                        planNedType: planMM.planNedType,
                        planCorpCode: planMM.planCorpCode,
                        planCorpName: planMM.planCorpName,
                        planDeptCode: planMM.planDeptCode,
                        planDeptName: planMM.planDeptName,
                        planIsContract: planMM.planIsContract,
                        planProType: planMM.planProType,
                        planGoodType: planMM.planGoodType,
                        planAssessType: planMM.planAssessType,
                        planMeanCode: planMM.planMeanCode,
                        planMeanDetail: planMM.planMeanDetail,
                        planCostCode: planMM.planCostCode,
                        planCostDetail: planMM.planCostDetail,
                        planOrderCode: planMM.planOrderCode,
                        planOrderDetail: planMM.planOrderDetail,
                        planFactoryCode: planMM.planFactoryCode,
                        planFactoryName: planMM.planFactoryName,
                        planIsReturn: planMM.planIsReturn,
                        planUnderDate: planMM.planUnderDate,
                        createTime: planMM.createTime,
                        createName: planMM.createName
                    }
                    return programMM;
                }

                // 编辑方案明细
                function editRow() {
                    let gramMmRows = $('#dataGridDiv').datagrid('getSelected');
                    if (!gramMmRows) {
                        $.messager.alert('提示', '请选择一条数据');
                        return
                    }
                    if (+gramMmRows.status != '0') {
                        $.messager.alert('提示', '该方案已提交，不允许再次修改')
                        return
                    }
                    let index = $('#dataGridDiv').datagrid('getRowIndex', gramMmRows);
                    setProgramDetailWindowValue(index, 'edit');
                }

                // 保存
                function savePurchasePrograms(action) {
                    if (!endEditing()) {
                        return;
                    }
                    $('#planMmDetailList').datagrid('acceptChanges');
                    if (!$('#programForm').form('validate')) {
                        alert('有未填写数据！');
                        return;
                    }
                    let gramStatus = '0'
                    if (action == 'submit') {
                        gramStatus = '1'
                    }
                    // 1.首先关闭编辑器
                    $('#planMmDetailList').datagrid('endEdit', editIndex);
                    editIndex = undefined;
                    // 2.获取物资明细
                    const gramMmRows = $('#planMmDetailList').datagrid('getRows');
                    // 3.获取供应商明细
                    const purchaseSupplies = $('#suppliesList').datagrid('getRows');
                    // 调用后台接口保存
                    let PurchaseProgramVO = {
                        gramId: $('#gramId').val(),
                        gramCode: $('#gramCode').val(),
                        gramName: $('#gramName').val(),
                        gramIsInside: $('#gramIsInside').combobox('getValue'),
                        gramContractType: $('#gramContractType').combobox('getValue'),
                        gramBudget: $('#gramBudget').val(),
                        gramMode: $('#gramMode').combobox('getValue'),
                        gramDeptCode: $('#gramDeptCode').val(),
                        gramDeptName: $('#gramDeptName').val(),
                        gramIsUrgent: $('#gramIsUrgent').combobox('getValue'),
                        gramChoseMode: $('#gramChoseMode').textbox('getValue'),
                        gramDealMode: $('#gramDealMode').combobox('getValue'),
                        //  createTime: $('#createTime').val(),
                        //  createName: $('#createName').val(),
                        //   updateName: $('#updateName').val(),
                        //   updateTime: $('#updateTime').val(),
                        gramFlowType: $('#gramFlowType').val(),
                        gramExplain: $('#gramExplain').val(),
                        status: gramStatus,
                        purchaseProgramMms: gramMmRows,
                        purchaseSupplies: purchaseSupplies
                    }
                    console.log('PurchaseProgramVO...', PurchaseProgramVO)
                    $.ajax({
                        type: "post",
                        dataType: "json",
                        url: 'rd/program/saveProgram',
                        data: JSON.stringify(PurchaseProgramVO),
                        contentType: "application/json",
                        success: function (data) {
                            if (data.code != 60200) {
                                $.messager.alert('消息', data.msg);
                            } else {
                                $.messager.alert('消息', data.msg);
                                // bingGrid();

                                var dg = $('#dataGridDiv');
                                dg.datagrid('enableFilter');
                                dg.datagrid('load');
                                $('#gramDetailWindow').window('close');
                            }
                        },
                        error: function (XMLHttpRequest, textStatus, errorThrown) {
                            $.messager.alert('消息', errorThrown);
                        }
                    })
                }

                // 采购方案详情窗口赋值
                function setProgramDetailWindowValue(index, action) {
                    const rows = $('#dataGridDiv').datagrid('getRows');
                    let row = rows[index];
                    if (+row.status == '0') {
                        $('#tabs').tabs({
                            tools: [{
                                text: '保存',
                                iconCls: 'icon-save',
                                handler: function () {
                                    savePurchasePrograms('save');
                                }
                            }, {
                                text: '提交',
                                iconCls: 'icon-ok',
                                handler: function () {
                                    savePurchasePrograms('submit')
                                }
                            }]
                        })
                    } else {
                        $('#tabs').tabs({
                            tools: []
                        })
                    }
                    $.ajax({
                        type: 'get',
                        dataType: 'json',
                        url: 'rd/program/selectProInfo?gramId=' + row.gramId,
                        success: function (data) {
                            if (data.code != 60200) {
                                $.messager.alert('消息', data.msg);
                            } else {
                                let item = data.data
                                $('#gramId').val(item.gramId)
                                $('#gramCode').val(item.gramCode);
                                $('#gramName').val(item.gramName);
                                $('#gramIsInside').combobox('setValue', item.gramIsInside);
                                $('#gramContractType').combobox('setValue', item.gramContractType);
                                $('#gramBudget').val(item.gramBudget);
                                $('#gramMode').combobox('setValue', item.gramMode);
                                $('#gramDeptCode').val(item.gramDeptCode);
                                $('#gramDeptName').val(item.gramDeptName);
                                $('#gramIsUrgent').combobox('setValue', item.gramIsUrgent);
                                $('#gramChoseMode').textbox('setValue', item.gramChoseMode);
                                $('#gramDealMode').combobox('setValue', item.gramDealMode);
                                $('#gramFlowType').val(item.gramFlowType);
                                // $('#createTime').val(item.createTime);
                                //   $('#createName').val(item.createName);
                                //  $('#updateName').val(item.updateName);
                                //  $('#updateTime').val(item.updateTime);
                                $('#gramExplain').val(item.gramExplain);
                                // 构建物资明细的dataGrid
                                for (var mm of item.purchaseProgramMms) {
                                    if (!mm.gramPurNum) {
                                        mm.gramPurNum = mm.planPurNum;
                                    }
                                }
                                setMmsDatagird(item.purchaseProgramMms, action);
                                // 构建供应商明细的datagrid
                                setSuppliesDatagird(item.purchaseSupplies, action);

                            }
                        },
                        error: function (XMLHttpRequest, textStatus, errorThrown) {
                            $.messager.alert('消息', errorThrown);
                        }
                    })
                    $('#gramDetailWindow').window('open');
                    $('#tabs').tabs('select', 0);
                }

                /**
                 * 删除采购方案
                 * @param data
                 */
                function delRow() {
                    const row = $('#dataGridDiv').datagrid('getSelected');
                    if (!row) {
                        $.messager.alert('消息', '请选择要删除的数据');
                        return
                    }
                    if (+row.status == '1') {
                        $.messager.alert('消息', '无法删除已提交的方案');
                        return
                    }
                    $.ajax({
                        type: 'delete',
                        dataType: 'json',
                        url: 'rd/program/delProgram?gramId=' + row.gramId,
                        success: function (data) {
                            if (data.code != 60200) {
                                $.messager.alert('消息', data.msg);
                            } else {
                                $.messager.alert('消息', data.msg);
                                // bingGrid();
                                var dg = $('#dataGridDiv');
                                dg.datagrid('enableFilter');
                                dg.datagrid('load');
                            }
                        },
                        error: function (XMLHttpRequest, textStatus, errorThrown) {
                            $.messager.alert('消息', errorThrown);
                        }
                    })

                }

                // 构建物资明细dataGrid
                function setMmsDatagird(data, action) {
                    var mmToolBar = [];
                    if (action == 'detail') {
                        mmToolBar = [];
                    } else {
                        // 物资列表的按钮工具栏
                        mmToolBar = [{
                            text: '新增',
                            iconCls: 'icon-add',
                            handler: function () {
                                // 打开计划物资的弹窗
                                selectMMWindow();
                            }
                        }, '-', {
                            text: '移除',
                            iconCls: 'icon-remove',
                            handler: function () {
                                $.messager.confirm("提示", "确认移除?", function (e) {
                                    if (e) {
                                        removeMm('mm');
                                    }
                                })
                            }
                        }];
                    }
                    let planMmDetailList = $('#planMmDetailList')
                    planMmDetailList.datagrid({
                        //             fitColumns: true,
                        data: data,
                        iconCls: 'icon-application',
                        clientPaging: false,
                        filterDelay: 0,
                        width: 860,
                        height: 300,
                        nowrap: true,
                        rownumbers: true,
                        animate: false,
                        collapsible: false,
                        singleSelect: true,
                        frozenColumns: [
                            [{
                                field: 'mmId',
                                title: '物资id',
                                hidden: true,
                                formatter: function (val, rec) {
                                    return unescape(val ? val : '')
                                }
                            },
                            {
                                field: 'gramId',
                                title: '方案id',
                                hidden: true,
                                sortable: true,
                                formatter: function (val, rec) {
                                    return unescape(val ? val : '')
                                }
                            },
                            {
                                field: 'mmCode',
                                title: '物资编码',
                                sortable: true,
                                formatter: function (val, rec) {
                                    return unescape(val ? val : '')
                                }
                            }, {
                                field: 'mmDetail',
                                title: '物资描述',
                                sortable: true,
                                width: 210,
                                formatter: function (val, rec) {
                                    if (val) {
                                        return '<span title="' + val + '">' + val + '</span>';
                                    } else {
                                        return val;
                                    }
                                }
                            },
                            ]
                        ],
                        columns: arrMMColmus,
                        toolbar: mmToolBar,
                        onEndEdit: function (index, row) { },
                        // 行单击事件
                        onClickRow: function (index, rowData) {
                            if (+rowData.status == 1) {
                                return $.messager.alert('提示', '该条数据已提交，不可进行编辑');
                            }
                            if (editIndex != index) {
                                if (endEditing()) {
                                    $('#planMmDetailList').datagrid('selectRow', index).datagrid('beginEdit', index);
                                    editIndex = index;
                                } else {
                                    $('#planMmDetailList').datagrid('selectRow', editIndex);
                                }
                            } else if (editIndex != undefined) { //如果不相等，说明已经打开编辑器了，需要关闭编辑器
                                $('#planMmDetailList').datagrid('endEdit', editIndex);
                                editIndex = undefined;
                            }
                        },
                        onLoadSuccess: function () {

                        }
                    });
                }

                // 构建供应商明细dataGrid
                function setSuppliesDatagird(data, action) {
                    var supplyToolBar = [];
                    if (action == 'detail') {
                        supplyToolBar = [];
                    } else {
                        // 供应商列表的按钮工具栏
                        supplyToolBar = [{
                            text: '新增',
                            iconCls: 'icon-add',
                            handler: function () {
                                setSuppliesDatagirdBySelection();
                            }
                        }, '-', {
                            text: '移除',
                            iconCls: 'icon-remove',
                            handler: function () {
                                $.messager.confirm("提示", "确认移除?", function (e) {
                                    if (e) {
                                        removeMm('supply')
                                    }
                                })
                            }
                        }];
                    }
                    let suppliesList = $('#suppliesList')
                    suppliesList.datagrid({
                        fitColumns: true,
                        data: data,
                        iconCls: 'icon-application',
                        remoteFilter: true,
                        clientPaging: false,
                        filterDelay: 0,
                        width: 860,
                        height: 300,
                        nowrap: true,
                        rownumbers: true,
                        animate: false,
                        collapsible: false,
                        singleSelect: true,
                        frozenColumns: [[{
                            field: 'infId',
                            title: '供应商id',
                            hidden: true,
                            sortable: true,
                            formatter: function (val, rec) {
                                return unescape(val ? val : '')
                            }
                        },
                        {
                            field: 'gramId',
                            title: '方案id',
                            hidden: true,
                            sortable: true,
                            width: 100,
                            formatter: function (val, rec) {
                                return unescape(val ? val : '')
                            }
                        },
                        {
                            field: 'infCode',
                            title: '供应商编码',
                            sortable: true,
                            formatter: function (val, rec, index) {
                                return unescape(val ? val : '')
                            }
                        }, {
                            field: 'infName',
                            title: '名称',
                            sortable: true,
                            width: 200,
                            formatter: function (val, rec) {
                                return unescape(val ? val : '')
                            }
                        }]],
                        columns: [
                            [
                                {
                                    field: 'infCreditCode',
                                    title: '社会信用代码',
                                    sortable: true,
                                    formatter: function (val, rec) {
                                        return val ? unescape(val) : '';
                                    }
                                }, {
                                    field: 'infCountry',
                                    title: '国家',
                                    sortable: true,
                                    formatter: function (val, rec) {
                                        return val ? unescape(val) : '';
                                    }
                                }, {
                                    field: 'infProvince',
                                    title: '地区',
                                    sortable: true,
                                    formatter: function (val, rec) {
                                        return val ? unescape(val) : '';
                                    }
                                }, {
                                    field: 'infCity',
                                    title: '地市',
                                    sortable: true,
                                    formatter: function (val, rec) {
                                        return val ? unescape(val) : '';
                                    }
                                }, {
                                    field: 'infAddress',
                                    title: '单位地址',
                                    sortable: true,
                                    formatter: function (val, rec) {
                                        return val ? unescape(val) : '';
                                    }
                                }, {
                                    field: 'infMobile',
                                    title: '手机',
                                    sortable: true,
                                    formatter: function (val, rec) {
                                        return val ? unescape(val) : '';
                                    }
                                }, {
                                    field: 'infContactsName',
                                    title: '联系人姓名',
                                    sortable: true,
                                    formatter: function (val, rec) {
                                        return val ? unescape(val) : '';
                                    }
                                }
                            ]
                        ],
                        toolbar: supplyToolBar,
                        onEndEdit: function (index, row) { },
                        onLoadSuccess: function () {

                        }
                    });

                }

                // 导出采购方案列表
                function exportList() {
                    let startTime = $('#startTime').datebox('getValue')
                    let endTime = $('#endTime').datebox('getValue')
                    let status = $('#gramStatus').combobox('getValue')

                    var form = [];
                    form.push('startTime=' + (startTime ? startTime : ''));
                    form.push('endTime=' + (endTime ? endTime : ''));
                    form.push('status=' + (status ? status : ''));
                    let url = 'api/rd/program/exportProgram'
                    let xhr = new XMLHttpRequest()
                    xhr.open('POST', url, true)
                    xhr.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
                    xhr.setRequestHeader('token', localStorage.getItem('token'));
                    xhr.setRequestHeader('userType', localStorage.getItem('userType'));
                    xhr.responseType = 'blob'
                    xhr.onload = function () {
                        if (this.status == 200) {
                            let blob = this.response
                            let reader = new FileReader()
                            reader.readAsDataURL(blob)
                            reader.onload = function (e) {
                                let a = document.createElement('a')
                                a.download = '采购方案' + new Date().toLocaleDateString() + '-' + new Date().toLocaleTimeString() + '.xlsx'
                                a.href = e.target.result
                                $('body').append(a)
                                a.click()
                                $(a).remove()
                            }
                        }
                    }
                    xhr.send(form.join('&'))
                }

                // 按时间查询采购方案列表
                function selectList() {
                    var startTime = $('#startTime').datebox('getValue')
                    var endTime = $('#endTime').datebox('getValue')
                    var gramStatus = $('#gramStatus').combobox('getValue');
                    var dg = $('#dataGridDiv');
                    dg.datagrid('enableFilter');
                    dg.datagrid('load', {
                        startTime: startTime ? startTime : '',
                        endTime: endTime ? endTime : '',
                        status: gramStatus ? gramStatus : ''
                    });
                }


                // 构建采购方案datagrid
                function bingGrid() {
                    var dg = $('#dataGridDiv');
                    var gramStatus = $('#gramStatus').combobox('getValue');
                    dg.datagrid({
                        title: '采购方案',
                        // data: gridData,
                        queryParams: {
                            startTime: $('#startTime').datebox('getValue') ? $('#startTime').datebox('getValue') : '',
                            endTime: $('#endTime').datebox('getValue') ? $('#endTime').datebox('getValue') : '',
                            status: gramStatus ? gramStatus : ''
                        },
                        // fitColumns: false,
                        fit: false,
                        url: 'rd/program/selectProgramList',
                        iconCls: 'icon-application',
                        clientPaging: false,
                        remoteFilter: true,
                        filterDelay: 0,
                        width: getWidth(0.98),
                        height: 415,
                        nowrap: true,
                        rownumbers: true,
                        animate: false,
                        collapsible: false,
                        singleSelect: true,
                        pageSize: 10,
                        remoteSort: true,
                        pagination: true,
                        frozenColumns: [
                            [{
                                field: 'gramCode',
                                title: '方案编码',
                                sortable: true,
                                formatter: function (val, rec, index) {
                                    return '<a href="javascript:void(0)" onclick="setProgramDetailWindowValue(' +
                                        index + ',' + "'detail'" + ')">' + (val ? val : '') + '</a>'
                                }
                            }, {
                                field: 'gramName',
                                title: '方案名称',
                                sortable: true,
                                formatter: function (val, rec) {
                                    return unescape(val ? val : '')
                                }
                            },]
                        ],
                        columns: [
                            [{
                                field: 'gramId',
                                title: '采购方案ID',
                                hidden: true,
                                sortable: true,
                                width: 100,
                                formatter: function (val, rec) {
                                    return unescape(val ? val : '')
                                }
                            },

                            {
                                field: 'gramMode',
                                title: '采购方式',
                                sortable: true,
                                formatter: function (val, rec) {
                                    return unescape(val ? val : '')
                                }
                            },

                            // {
                            //     field: 'gramIsInside',
                            //     title: '内部外部',
                            //     sortable: true,
                            //     formatter: function (val, rec) {
                            //         return unescape(val == '1' ? '内部' : '外部')
                            //     }
                            // },
                            // {
                            //     field: 'gramFlow',
                            //     title: '业务流程',
                            //     sortable: true,
                            //     formatter: function (val, rec) {
                            //         return unescape(val ? val : '')
                            //     }
                            // },
                            // {
                            //     field: 'gramChoseMode',
                            //     title: '选商方式',
                            //     sortable: true,
                            //     formatter: function (val, rec) {
                            //         return unescape(val ? val : '')
                            //     }
                            // }, 
                            {
                                field: 'gramDealMode',
                                title: '成交方式',
                                sortable: true,
                                formatter: function (val, rec) {
                                    return unescape(val ? val : '')
                                }
                            },
                            {
                                field: 'gramDeptCode',
                                title: '采购单位编码',
                                sortable: true,
                                formatter: function (val, rec) {
                                    return unescape(val ? val : '')
                                }
                            },
                            // {
                            //     field: 'gramContractType',
                            //     title: '合同类型',
                            //     sortable: true,
                            //     formatter: function (val, rec) {
                            //         return unescape(val == '1' ? '非贸易' : '贸易')
                            //     }
                            // },
                            {
                                field: 'gramDeptName',
                                title: '采购单位名称',
                                sortable: true,
                                formatter: function (val, rec) {
                                    return unescape(val ? val : '')
                                }
                            },
                            // {
                            //     field: 'gramBudget',
                            //     title: '预算金额',
                            //     sortable: true,
                            //     formatter: function (val, rec) {
                            //         return unescape(val ? val : '')
                            //     }
                            // },
                            // {
                            //     field: 'gramIsUrgent',
                            //     title: '是否急用',
                            //     sortable: true,
                            //     formatter: function (val, rec) {
                            //         return unescape(val == '1' ? '是' : '否')
                            //     }
                            // },
                            {
                                field: 'createName',
                                title: '制单人名称',
                                sortable: true,
                                formatter: function (val, rec) {
                                    return unescape(val ? val : '')
                                }
                            },
                            {
                                field: 'createTime',
                                title: '创建时间',
                                sortable: true,
                                formatter: function (val, rec) {
                                    return unescape(val ? val : '')
                                }
                            },
                            {
                                field: 'updateTime',
                                title: '更新时间',
                                sortable: true,
                                formatter: function (val, rec) {
                                    return unescape(val ? val : '')
                                }
                            },
                            {
                                field: 'status',
                                title: '状态',
                                sortable: true,
                                formatter: function (val, rec) {
                                    let status = val ? val : '';
                                    return unescape(status == '0' ? '待提交' : (status == '2' ? '已完成' : '已提交'));
                                }
                            }
                            ]
                        ],
                        toolbar: [{
                            text: '新增',
                            iconCls: 'icon-add',
                            handler: function () {
                                addProgram('add');
                            }
                        }, '-',
                        {
                            text: '编辑',
                            iconCls: 'icon-edit',
                            handler: function () {
                                editRow();
                            }
                        },
                            '-',
                        {
                            text: '删除',
                            iconCls: 'icon-remove',
                            handler: function () {
                                $.messager.confirm("提示", "确认删除?", function (e) {
                                    if (e) {
                                        delRow();
                                    }
                                })
                            }
                        },
                            //                             '-', {
                            //                             text: '撤回',
                            //                             iconCls: 'icon-undo',
                            //                             handler: function () {
                            //                                 $.messager.confirm("提示", "确认撤回?", function (e) {
                            //                                     if (e) {
                            //                                         gramBackUp();
                            //                                     }
                            //                                 })
                            //                             }
                            //                         }
                        ],
                        onLoadSuccess: function () {

                        },
                        onDblClickRow: function () {
                            editRow();
                        }
                    });
                    dg.datagrid('enableFilter');
                }

                /**
                 * 根据选择的planId 查询计划物资列表
                 */
                /*    function transferPlanId() {
                        let planDataGridWindow = $('#planDataGridWindow');
                        const planRow = planDataGridWindow.datagrid('getSelected');
                        if (!planRow) {
                            $.messager.alert('提示', '请至少选择一条采购计划')
                            return
                        }
                        let planId = planRow.planId
                        let mmDataGrid = $('#mmDataGrid');
                        mmDataGrid.datagrid('enableFilter');
                        mmDataGrid.datagrid('load', {
                            planId: planId
                        });
                        $('#selectPlanWindow').window('close');
                    }*/

                /**
                 * 条件查询计划物资明细的方法
                 */
                function selectMMListByParams() {
                    let startTime = $('#startMMTime').datebox('getValue')
                    let endTime = $('#endMMTime').datebox('getValue')
                    let mmDataGrid = $('#mmDataGrid');
                    mmDataGrid.datagrid('enableFilter');
                    mmDataGrid.datagrid('load', {
                        startTime: startTime ? startTime : '',
                        endTime: endTime ? endTime : '',
                        status: '1'
                    });
                }

                /**
                 * 条件查询待选供应商的方法
                 */
                function selectSupplyListByParams() {
                    //         let startTime = $('#startSupplyTime').datebox('getValue')
                    //         let endTime = $('#endSupplyTime').datebox('getValue')
                    let supplyDataGrid = $('#supplyDataGrid');
                    supplyDataGrid.datagrid('load', {
                        //             startTime: startTime ? startTime : '',
                        //             endTime: endTime ? endTime : '',
                        status: '1'
                    });
                    supplyDataGrid.datagrid('enableFilter');
                }

                /**
                 * 打开计划物资的弹窗
                 */
                function selectMMWindow() {
                    $('#mmDataGrid').datagrid('clearChecked');
                    selectMMListByParams();
                    $('#selectMMWindow').window('open');
                }

                function selectMMGrid() {
                    $('#startMMTime').datebox('setValue', getBeforeCurrentDate30Days());
                    $('#endMMTime').datebox('setValue', getNowFormatDate());
                    let mmDataGrid = $('#mmDataGrid');
                    mmDataGrid.datagrid({
                        queryParams: {
                            startTime: $('#startTime').datebox('getValue') ? $('#startTime').datebox('getValue') : '',
                            endTime: $('#endTime').datebox('getValue') ? $('#endTime').datebox('getValue') : '',
                            status: '1'
                        },
                        fitColumns: true,
                        url: 'rd/plan/selectPlanMmList?status=1',
                        iconCls: 'icon-application',
                        clientPaging: false,
                        checkOnSelect: true,
                        remoteFilter: true,
                        filterDelay: 0,
                        width: 860,
                        height: 300,
                        nowrap: true,
                        rownumbers: true,
                        animate: false,
                        collapsible: false,
                        singleSelect: false,
                        pageSize: 20,
                        pageList: [20, 30, 40, 50],
                        remoteSort: true,
                        pagination: true,
                        frozenColumns: [
                            [{
                                field: 'ck',
                                checkbox: true,
                                width: 100,
                            },
                            {
                                field: 'mmId',
                                title: '物资ID',
                                hidden: true,
                                width: 100,
                                formatter: function (val, rec) {
                                    return unescape(val ? val : '')
                                }
                            },
                            {
                                field: 'planId',
                                title: '采购计划ID',
                                hidden: true,
                                sortable: true,
                                width: 100,
                                formatter: function (val, rec) {
                                    return unescape(val ? val : '')
                                }
                            },
                            // {
                            //     field: 'planCode',
                            //     title: '计划编码',
                            //     sortable: true,
                            //     formatter: function (val, rec, index) {

                            //         return '<a href="javascript:void(0)" onclick="setPlanDetailWindowValue(' +
                            //             index + ')">' + (val ? val : '') + '</a>'
                            //     }
                            // }, 
                            {
                                field: 'mmCode',
                                title: '物资编码',
                                sortable: true,
                                formatter: function (val, rec) {
                                    return unescape(val ? val : '')
                                }
                            },
                            {
                                field: 'mmDetail',
                                title: '物资描述',
                                sortable: true,
                                width: 210,
                                formatter: function (val, rec) {
                                    if (val) {
                                        return '<span title="' + val + '">' + val + '</span>';
                                    } else {
                                        return val;
                                    }
                                }
                            },
                            ]
                        ],
                        columns: [
                            [
                                {
                                    field: 'planPurNum',
                                    title: '采购计划量',
                                    sortable: true,
                                    formatter: function (val, rec) {
                                        return unescape(val ? val : '')
                                    }
                                },
                                {
                                    field: 'planStockNum',
                                    title: '剩余量',
                                    sortable: true,
                                    formatter: function (val, rec) {
                                        return unescape(val ? val : '')
                                    }
                                },

                                {
                                    field: 'planCorpCode',
                                    title: '公司编码',
                                    sortable: true,
                                    formatter: function (val, rec) {
                                        return unescape(val ? val : '')
                                    }
                                },
                                {
                                    field: 'planCorpName',
                                    title: '公司名称',
                                    sortable: true,
                                    formatter: function (val, rec) {
                                        return unescape(val ? val : '')
                                    }
                                },
                                {
                                    field: 'mmUnit',
                                    title: '计量单位',
                                    sortable: true,
                                    formatter: function (val, rec) {
                                        return unescape(val ? val : '')
                                    }
                                },
                                {
                                    field: 'mmUnitDetail',
                                    title: '计量单位描述',
                                    sortable: true,
                                    formatter: function (val, rec) {
                                        return unescape(val ? val : '')
                                    }
                                },
                                {
                                    field: 'planOrderDetail',
                                    title: '订单描述',
                                    sortable: true,
                                    formatter: function (val, rec) {
                                        return unescape(val ? val : '')
                                    }
                                },
                                {
                                    field: 'planFactoryCode',
                                    title: '到货工厂',
                                    sortable: true,
                                    formatter: function (val, rec) {
                                        return unescape(val ? val : '')
                                    }
                                },
                                {
                                    field: 'planFactoryName',
                                    title: '到货工厂描述',
                                    sortable: true,
                                    formatter: function (val, rec) {
                                        return unescape(val ? val : '')
                                    }
                                },
                            ]
                        ],
                        toolbar: [{
                            text: '加入',
                            iconCls: 'icon-ok',
                            handler: function () {
                                addRowsToPlanMmDetailList();
                                //  editRow();
                            }
                        }, '-',
                            /* {
                                 text: '查看采购计划列表',
                                 iconCls: 'icon-search',
                                 handler: function () {
                                     setPlanGrid();
                                 }
                             }*/
                        ],
                        onEndEdit: function (index, row) { },
                        onLoadSuccess: function () {
                            // $('#selectMMWindow').window('open')
                        },
                        // 行单击事件
                        onClickRow: function (index, rowData) {
                            /* if (+rowData.status == 1) {
                                 return $.messager.alert('提示','该条数据已提交，不可进行编辑');
                             }
                             if (editIndex != index) {
                                 if (endEditing()) {
                                     $('#planMmDetailList').datagrid('selectRow', index).datagrid('beginEdit', index);
                                     editIndex = index;
                                 } else {
                                     $('#planMmDetailList').datagrid('selectRow', editIndex);
                                 }
                             } else if (editIndex != undefined) {//如果不相等，说明已经打开编辑器了，需要关闭编辑器
                                 $('#planMmDetailList').datagrid('endEdit', editIndex);
                                 editIndex = undefined;
                             }*/
                        }
                    })
                    mmDataGrid.datagrid('enableFilter');
                }

                /**
                 * 将选中的计划物资添加到物资列表
                 */
                function addRowsToPlanMmDetailList() {
                    let mmDataGrid = $('#mmDataGrid');
                    const checkedmmRows = mmDataGrid.datagrid('getChecked');
                    if (!checkedmmRows || checkedmmRows.length < 1) {
                        $.messager.alert('提示', '请至少选择一条数据');
                        return
                    }
                    if (checkedmmRows.length > 0 && checkedmmRows != null) {
                        // 增加判断：需求类型、采购组织、公司代码保持一致
                        let flag = checkPropertyOfSelectedMm(checkedmmRows)
                        if (!flag) {
                            $.messager.alert('提示', '请确定所选物资的需求类型、采购组织、公司代码、资产\\物料\\费用、是否合同、到货工厂保持一致')
                            return
                        }
                        // 与已添加进物资明细的数据做对比，planId相同的不再重复添加
                        const planMmrows = $('#planMmDetailList').datagrid('getRows')
                        for (let i in checkedmmRows) {
                            // planMM 转换programMM
                            let programMM = convertToPrograMM(checkedmmRows[i])
                            if (planMmrows.length > 0 && planMmrows != null) {
                                const result = planMmrows.some((item, index, arr) => {
                                    return item.planId == programMM.planId
                                })
                                if (!result) {
                                    $('#planMmDetailList').datagrid('insertRow', {
                                        row: programMM
                                    })
                                }
                            } else {
                                $('#planMmDetailList').datagrid('insertRow', {
                                    row: programMM
                                })
                            }

                        }
                    }
                    $('#selectMMWindow').window('close');
                }

                function checkPropertyOfSelectedMm(selectedMMArr) {
                    let flag = true
                    // 需求类型
                    let planNedType = ''
                    // 使用公司
                    let planCorpCode = ''
                    // 采购组织
                    let planDeptCode = ''
                    //资产/费用/存货
                    let planGoodType = ''
                    // 是否合同
                    let isContract = ''
                    // 到货工厂
                    let planFactoryCodeTem = ''
                    const rows = $('#planMmDetailList').datagrid('getRows')
                    // 编辑时
                    if (rows && rows.length > 0) {
                        let row = rows[0]
                        planNedType = row.planNedType
                        planCorpCode = row.planCorpCode
                        planDeptCode = row.planDeptCode
                        planGoodType = row.planGoodType
                        isContract = row.planIsContract
                        planFactoryCodeTem = row.planFactoryCode
                        for (let i in selectedMMArr) {
                            let selectedMM = selectedMMArr[i]
                            if (planNedType != selectedMM.planNedType) {
                                flag = false
                                return
                            }
                            if (planCorpCode != selectedMM.planCorpCode) {
                                flag = false
                                return
                            }
                            if (planDeptCode != selectedMM.planDeptCode) {
                                flag = false
                                return
                            }
                            if (planGoodType != selectedMM.planGoodType) {
                                flag = false
                                return
                            }
                            if (isContract != selectedMM.isContract) {
                                flag = false
                                return
                            }
                            if (planFactoryCodeTem != selectedMM.planFactoryCode) {
                                flag = false
                                return
                            }
                        }
                    } else {
                        // 新增时
                        if (selectedMMArr.length > 1) {
                            planNedType = selectedMMArr[0].planNedType
                            planCorpCode = selectedMMArr[0].planCorpCode
                            planDeptCode = selectedMMArr[0].planDeptCode
                            planGoodType = selectedMMArr[0].planGoodType
                            isContract = selectedMMArr[0].planIsContract
                            planFactoryCodeTem = selectedMMArr[0].planFactoryCode
                            for (let i in selectedMMArr) {
                                let selectedMM = selectedMMArr[i]
                                if (planNedType != selectedMM.planNedType) {
                                    flag = false
                                    return
                                }
                                if (planCorpCode != selectedMM.planCorpCode) {
                                    flag = false
                                    return
                                }
                                if (planDeptCode != selectedMM.planDeptCode) {
                                    flag = false
                                    return
                                }
                                if (planGoodType != selectedMM.planGoodType) {
                                    flag = false
                                    return
                                }
                                if (isContract != selectedMM.isContract) {
                                    flag = false
                                    return
                                }
                                if (planFactoryCodeTem != selectedMM.planFactoryCode) {
                                    flag = false
                                    return
                                }
                            }
                        }

                    }
                    return flag

                }

                // 设置采购计划dataGrid
                /* function setPlanGrid() {
                                 let startTime = $('#startPlanTime').datebox('getValue')
                                 let endTime = $('#endPlanTime').datebox('getValue')
                                 let planDataGridWindow = $('#planDataGridWindow');
                                 planDataGridWindow.datagrid({
                                     queryParams: {
                                         startTime: startTime ? startTime : '',
                                         endTime: endTime ? endTime : ''
                                     },
                                     fitColumns: true,
                                     url: 'rd/plan/selectPlanList?status=1',
                                     iconCls: 'icon-application',
                                     clientPaging: false,
                                     remoteFilter: true,
                                     filterDelay: 0,
                                     width: 860,
                                     height: 300,
                                     nowrap: true,
                                     rownumbers: true,
                                     animate: false,
                                     collapsible: false,
                                     singleSelect: true,
                                     pageSize: 10,
                                     remoteSort: true,
                                     pagination: true,
                                     columns: [[
                                         {
                                             field: 'ck',
                                             checkbox: true,
                                             width: 100,
                                             formatter: function (val, rec) {
                                                 return unescape(val ? val : '')
                                             }
                                         },
                                         {
                                             field: 'planId',
                                             title: '采购计划ID',
                                             hidden: true,
                                             sortable: true,
                                             width: 100,
                                             formatter: function (val, rec) {
                                                 return unescape(val ? val : '')
                                             }
                                         },
                                         {
                                             field: 'planCode',
                                             title: '计划编码',
                                             sortable: true,
                                             formatter: function (val, rec, index) {
                                                 return unescape(val ? val : '');
                                             }
                                         }, {
                                             field: 'planName',
                                             title: '计划名称',
                                             sortable: true,
                                             formatter: function (val, rec) {
                                                 return unescape(val ? val : '')
                                             }
                                         }, {
                                             field: 'planCategory',
                                             title: '计划类别',
                                             sortable: true,
                                             formatter: function (val, rec) {
                                                 return unescape(val ? val : '')
                                             }
                                         },
                                         {
                                             field: 'planType',
                                             title: '计划类型',
                                             sortable: true,
                                             formatter: function (val, rec) {
                                                 return unescape(val ? val : '')
                                             }
                                         },
                                         {
                                             field: 'planMmType',
                                             title: '物资类型',
                                             sortable: true,
                                             formatter: function (val, rec) {
                                                 return unescape(val ? val : '')
                                             }
                                         },
                                         {
                                             field: 'planProCode',
                                             title: '项目编码',
                                             sortable: true,
                                             formatter: function (val, rec) {
                                                 return unescape(val ? val : '')
                                             }
                                         },
                                         {
                                             field: 'planProName',
                                             title: '项目名称',
                                             sortable: true,
                                             formatter: function (val, rec) {
                                                 return unescape(val ? val : '')
                                             }
                                         },
                                         {
                                             field: 'planPurType',
                                             title: '采购类型',
                                             sortable: true,
                                             formatter: function (val, rec) {
                                                 return unescape(val ? val : '')
                                             }
                                         },
                                         {
                                             field: 'planDeptCode',
                                             title: '使用单位编码',
                                             sortable: true,
                                             formatter: function (val, rec) {
                                                 return unescape(val ? val : '')
                                             }
                                         },
                                         {
                                             field: 'planDeptName',
                                             title: '使用单位名称',
                                             sortable: true,
                                             formatter: function (val, rec) {
                                                 return unescape(val ? val : '')
                                             }
                                         },
                                         {
                                             field: 'planIsNeed',
                                             title: '是否急用',
                                             sortable: true,
                                             formatter: function (val, rec) {
                                                 return unescape(val ? val : '')
                                             }
                                         },
                                         {
                                             field: 'createName',
                                             title: '制单人名称',
                                             sortable: true,
                                             formatter: function (val, rec) {
                                                 return unescape(val ? val : '')
                                             }
                                         },
                                         {
                                             field: 'createTime',
                                             title: '创建时间',
                                             sortable: true,
                                             formatter: function (val, rec) {
                                                 return unescape(val ? val : '')
                                             }
                                         },
                                         {
                                             field: 'updateTime',
                                             title: '更新时间',
                                             sortable: true,
                                             formatter: function (val, rec) {
                                                 return unescape(val ? val : '')
                                             }
                                         },
                                         {
                                             field: 'status',
                                             title: '状态',
                                             sortable: true,
                                             formatter: function (val, rec) {
                                                 let status = val ? val : '';
                                                 return unescape(status == '0' ? '待提交' : '已提交');
                                             }
                                         }
                                     ]],
                                     toolbar: [
                                         {
                                             text: '确定',
                                             iconCls: 'icon-ok',
                                             handler: function () {
                                                 transferPlanId()
                                                 //  editRow();
                                             }
                                         }, '-', {
                                             text: '撤回',
                                             iconCls: 'icon-undo',
                                             handler: function () {
                                                 planBackUp();
                                             }
                                         }
                                     ],
                                     onLoadSuccess: function () {
                                         $('#selectPlanWindow').window('open');
                                     },
                                     onDblClickRow: function () {
                        
                                     }
                                 });
                                 planDataGridWindow.datagrid('enableFilter');
                        
                             }*/
            </script>

            </html>